/*
 * CollectGoodsPresenter.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.collect.goods;

import com.yidiandao.business.R;
import com.yidiandao.business.data.event.CollectGoodsEvent;
import com.yidiandao.business.data.model.CollectionModel;
import com.yidiandao.business.data.model.ICollectionModel;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;

/** 我的 收藏商品 */
public class CollectGoodsPresenter implements CollectGoodsContract.Presenter {

    private final CollectionModel mCollectionModel;
    private CollectGoodsContract.View mView;
    private int totalPage = 1; //数据总页数

    private int currentPage = 1; //当前页数

    private boolean isLoading; //是否正在加载中

    private ArrayList<DisplayListItem> mDisplayListItems = new ArrayList<>();

    public CollectGoodsPresenter(CollectGoodsContract.View view) {
        mView = view;
        mView.setPresenter(this);
        mCollectionModel = new CollectionModel();
    }

    @Override
    public void start() {
        getCollectionData(true);
    }

    @Override
    public void getCollectionData(final boolean isRefresh) {

        if (isRefresh) {
            currentPage = 1;
            totalPage = 1;
        }
        if (currentPage > totalPage || isLoading) {
            //没有更多数据了
            return;
        }
        isLoading = true;
        mCollectionModel.getCollectionGoodsData(
                mView.getPagerTag(),
                currentPage,
                new ICollectionModel.OnGetCollectionGoodsListener() {

                    @Override
                    public void onFailed() {
                        if (isRefresh) {
                            mView.refreshContent(SuperLoadingLayout.STATE_ERROR);
                        } else {
                            mView.loadMoreError();
                        }
                    }

                    @Override
                    public void onFinal() {
                        isLoading = false;
                    }

                    @Override
                    public void onGetGoodsListSuccess(
                            ArrayList<DisplayListItem> displayListItems, int total) {
                        if (isRefresh) {
                            mDisplayListItems.clear();
                            mView.refreshContent(mView.checkResultObject(displayListItems));
                            mView.setRefreshComplete();
                        }
                        totalPage = total;
                        currentPage++;
                        mDisplayListItems.addAll(displayListItems);
                        mView.setDisplayListItems(mDisplayListItems);
                        mView.loadMoreFinish(mDisplayListItems.size() == 0, currentPage <= total);
                    }
                });
    }

    @Override
    public void collectGoods(final CollectGoodsEvent event) {
        mCollectionModel.collectionGoods(
                mView.getPagerTag(),
                event.goodsId,
                1,
                new ICollectionModel.OnAccessNetListener() {
                    @Override
                    public void onSuccess() {
                        UIUtils.showToast(UIUtils.getString(R.string.shop_cancel_collect_success));
                        mDisplayListItems.remove(event.collectGoodsListItem);
                        start();
                    }
                });
    }
}
